<?php
/**
 * 推荐位后台管理
 * @author liubo <liubo@ilongyuan.com.cn>
 */
namespace Admin\Controller;

class PosController extends BaseManageController {
    /**
    *控制操作的模型表
    */
    protected $modelName = 'Pos';

    /**
    *删除类型的设置 true|直接删除， false|设置数据状态
    */
    protected $is_del   = false;

    /**
    *跳转的Url
    */
    protected $jumpAct  = 'Pos/index';

    /**
    *操作index的前置操作
    *---设置数据筛选条件---
    */
    public function _before_index()
    {
        $map = array();//筛选条件
        $map['status'] = array('egt',0);
        $search = trim(I('get.name', '', 'strip_tags'));
        if($search){
            $map['name']    =   array('like', (string)$search);
        }
        $this->where = $map;
    }

    public function format_lists(&$lists) {
        $types = C('POS_TYPES');
        foreach ($lists as &$value) {
            $value['type_str'] = $types[$value['type']];
        }
    }

    public function _before_add()
    {
        if (IS_POST && $this->checkCodeExists(I('code'))) {
            $this->error('CODE已存在');
        }
    }

    public function _before_edit($id)
    {
        if (IS_POST && $this->checkCodeExists(I('code'), $id)) {
            $this->error('CODE已存在');
        }
    }

    /**
     * 检查code是否存在
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    private function checkCodeExists($code, $id = 0)
    {
        $map['code'] = $code;
        $id && $map['id'] = $id;
        $pos = service('Pos')->where($map)->find();
        return !empty($pos);
    }

    /**
     * 推荐位内容
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function items()
    {
        $id = I('request.id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        if (IS_POST) {
            $sort_list = I('sort');
            if (is_array($sort_list)) {
                foreach ($sort_list as $key => $value) {
                    service('PosData')->save([
                        'id' => $key,
                        'sort' => intval($value)
                    ]);
                }
                $this->success('操作成功!', U('Pos/items', ['id' => $id]));
            }
        }
        $this->pos = service('Pos')->find($id);
        $types = C('POS_TYPES');
        $this->type_str = $types[$this->pos['type']];

        $lists = service('PosData')->where(['pos_id' => $id])->order('sort asc')->select();
        switch ($this->pos['type']) {
            case 'video':
                $event = event('Video');
                $field = 'title';
                break;
            case 'user':
                $event = event('User');
                $field = 'nickname';
                break;
            case 'race':
                $event = event('RaceGroup');
                $field = 'title';
                break;
            case 'goods':
                $event = event('MallGoods');
                $field = 'name';
                break;
            case 'news':
                $event = event('News');
                $field = 'title';
                break;
        }

        foreach ($lists as &$value) {
            $info = $event->info($value['target_id']);
            $value['name'] = $info[$field];
        }
        $this->assign('_list',$lists);
        $this->display();
    }
}